package com.djx.commons.config.shiro;

import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.mgt.SecurityManager;


import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfiguration {
    @Bean
    public MyRealm createRealm() {
        return new MyRealm();
    }

    @Bean
    public SecurityManager createSecurityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(this.createRealm());

        securityManager.setSessionManager(this.defaultWebSessionManager());
//        securityManager.setCacheManager(this.redisCacheManager());

        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean createShiroFilterFactoryBean() {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(this.createSecurityManager());

        //拦截器
        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        filters.put("anon", new AnonFilter());
        filters.put("authc", new CORSAuthenticationFilter());
        shiroFilterFactoryBean.setFilters(filters);

        //拦截路径
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/sys/login", "anon");
//        filterMap.put("/userController/findUser","anon");
        filterMap.put("/**", "authc");


        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(this.createSecurityManager());
        return advisor;
    }

//    //缓存管理器
//    public RedisCacheManager redisCacheManager(){
//        RedisCacheManager redisCacheManager = new RedisCacheManager();
//        RedisManager redisManager = new RedisManager();
//        redisManager.setPort(6379);
//        redisManager.setHost("127.0.0.1");
//        //redisManager.setPassword("");
//        redisCacheManager.setRedisManager(redisManager);
//        return redisCacheManager;
//    }

    //会话管理器
    public DefaultWebSessionManager defaultWebSessionManager(){
        //一旦使用DefaultWebSessionManager 那么就需要使用自定义的会话
        CustomSessionManager customSessionManager = new CustomSessionManager();

//        RedisManager redisManager = new RedisManager();
//        redisManager.setPort(6379);
//        redisManager.setHost("127.0.0.1");
        //redisManager.setPassword("");


//        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
//        redisSessionDAO.setRedisManager(redisManager);

        //因为CustomSessionManager继承了DefaultWebSessionManager,DefaultWebSessionManager继承了DefaultSessionManager
        //所以可以使用DefaultSessionManager中的setSessionDao方法来重写SessionDao
//        customSessionManager.setSessionDAO(redisSessionDAO);

        return customSessionManager;
    }
}
